;;; Copyright © 2018 Kei Kebreau <kkebreau@posteo.net>
;;;
;;; This program is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (ti-calc-tools)
  #:use-module (guix packages)
  #:use-module (gnu packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix gexp)
  #:use-module (guix utils)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages backup)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages gettext)
  #:use-module (gnu packages glib)
  #:use-module (gnu packages gnome)
  #:use-module (gnu packages gtk)
  #:use-module (gnu packages libusb)
  #:use-module (gnu packages multiprecision)
  #:use-module (gnu packages nettle)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python)
  #:use-module (gnu packages tls))

(define %patch-dir
  (string-append (if (current-filename)
                     (dirname (current-filename))
                     ".")
                 "/patches"))

(define tilp-version "1.18")

(define-public libticables
  (package
   (name "libticables")
   (version "1.3.5")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilp/tilp2-linux/"
                                "tilp2-" tilp-version "/"
                                name "2-" version ".tar.bz2"))
            (sha256
             (base32
              "08j5di0cgix9vcpdv7b8xhxdjkk9zz7fqfnv3l4apk3jdr8vcvqc"))))
   (build-system gnu-build-system)
   (propagated-inputs
    ;; Located in Requires.private of ticables2.pc.
    `(("glib" ,glib)))
   (native-inputs
    `(("autoconf" ,autoconf)
      ("automake" ,automake)
      ("gettext" ,gettext-minimal)
      ("libtool" ,libtool)
      ("pkg-config" ,pkg-config)))
   (inputs
    `(("libusb" ,libusb)))
   (arguments
    `(#:configure-flags '("--enable-libusb10") ; Build against libusb 1.0.
      #:phases
      (modify-phases %standard-phases
        (add-after 'install 'install-documentation
          (lambda* (#:key outputs #:allow-other-keys)
            (let* ((out (assoc-ref outputs "out"))
                   (doc (string-append out "/share/doc/"
                                       ,name "-" ,version)))
              (for-each (lambda (file)
                          (install-file file doc))
                        (find-files "docs/html")))
            #t)))))
   (home-page "http://lpg.ticalc.org/prj_tilp/")
   (synopsis "TI link cable library")
   (description
    "@code{libticables} is a library providing support for operations on Texas
Instruments calculators' link cables.  All link cables are supported
read/write.")
   (license license:gpl2+)))

(define-public libticonv
  (package
   (name "libticonv")
   (version "1.1.5")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilp/tilp2-linux/"
                                "tilp2-" tilp-version "/"
                                name "-" version ".tar.bz2"))
            (sha256
             (base32
              "0y080v12bm81wgjm6fnw7q0yg7scphm8hhrls9njcszj7fkscv9i"))))
   (build-system gnu-build-system)
   (propagated-inputs
    ;; Located in Requires.private of ticonv.pc.
    `(("glib" ,glib)))
   (native-inputs
    `(("autoconf" ,autoconf)
      ("automake" ,automake)
      ("libtool" ,libtool)
      ("pkg-config" ,pkg-config)))
   (arguments
    `(#:configure-flags '("--enable-iconv")
      #:phases
      (modify-phases %standard-phases
        (add-after 'install 'install-documentation
          (lambda* (#:key outputs #:allow-other-keys)
            (let* ((out (assoc-ref outputs "out"))
                   (doc (string-append out "/share/doc/"
                                       ,name "-" ,version)))
              (substitute* "docs/html/info.html"
                (("\\.\\./charsets/") "")
                (("<a href=\"\\.\\./charsets\">folder</a>") "folder"))
              (for-each (lambda (file)
                          (install-file file doc))
                        (append (find-files "docs/charsets")
                                (find-files "docs/html"))))
            #t)))))
   (home-page "http://lpg.ticalc.org/prj_tilp/")
   (synopsis "TI character set conversion library")
   (description
    "@code{libticonv} is a library providing support for operations on Texas
Instruments calculators' charsets.  All charsets for all calculators are
supported; the library is also able to convert from/to UTF-8/UTF-16.")
   (license license:gpl2+)))

(define-public libtifiles
  (package
   (name "libtifiles")
   (version "1.1.7")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilp/tilp2-linux/"
                                "tilp2-" tilp-version "/"
                                name "2-" version ".tar.bz2"))
            (sha256
             (base32
              "10n9mhlabmaw3ha5ckllxfy6fygs2pmlmj5v6w5v62bvx54kpils"))))
   (build-system gnu-build-system)
   (propagated-inputs
    ;; Located in Requires.private of tifiles2.pc.
    `(("libarchive" ,libarchive)
      ("libticonv" ,libticonv)
      ;; Nettle and zlib just seem to be needed by everything that depends on
      ;; libtifiles.
      ("nettle" ,nettle)
      ("zlib" ,zlib)))
   (native-inputs
    `(("autoconf" ,autoconf)
      ("automake" ,automake)
      ("gettext" ,gettext-minimal)
      ("libtool" ,libtool)
      ("pkg-config" ,pkg-config)))
   (arguments
    `(#:phases
      (modify-phases %standard-phases
        (add-after 'install 'install-documentation
          (lambda* (#:key outputs #:allow-other-keys)
            (let* ((out (assoc-ref outputs "out"))
                   (doc (string-append out "/share/doc/"
                                       ,name "-" ,version)))
              (for-each (lambda (file)
                          (install-file file doc))
                        (find-files "docs/html")))
            #t)))))
   (home-page "http://lpg.ticalc.org/prj_tilp/")
   (synopsis "TI file format library")
   (description
    "@code{libtifiles} is a library providing support for operations on Texas
Instruments calculators' files.  All formats for all calculators are supported
read/write; the library is also able to manipulate the group files, which means
it is able to group and ungroup variables into such group files.")
   (license license:gpl2+)))

(define-public libticalcs
  (package
   (name "libticalcs")
   (version "1.1.9")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilp/tilp2-linux/"
                                "tilp2-" tilp-version "/"
                                name "2-" version ".tar.bz2"))
            (sha256
             (base32
              "08c9wgrdnyqcs45mx1bjb8riqq81bzfkhgaijxzn96rhpj40fy3n"))))
   (build-system gnu-build-system)
   (propagated-inputs
    ;; Located in Requires or Requires.private of ticalcs2.pc.
    `(("libticables" ,libticables)
      ("libtifiles" ,libtifiles)))
   (native-inputs
    `(("autoconf" ,autoconf)
      ("automake" ,automake)
      ("gettext" ,gettext-minimal)
      ("libtool" ,libtool)
      ("pkg-config" ,pkg-config)))
   (arguments
    `(#:phases
      (modify-phases %standard-phases
        (add-after 'install 'install-documentation
          (lambda* (#:key outputs #:allow-other-keys)
            (let* ((out (assoc-ref outputs "out"))
                   (doc (string-append out "/share/doc/"
                                       ,name "-" ,version)))
              (for-each (lambda (file)
                          (install-file file doc))
                        (cons "docs/romdump.txt" (find-files "docs/html"))))
            #t)))))
   (home-page "http://lpg.ticalc.org/prj_tilp/")
   (synopsis "TI calculator communication library")
   (description
    "@code{libticalcs} is a library providing support for communication with
Texas Instruments calculators.  It implements the TI protocol for each type of
calculator, independently of the link cable used to establish the link. All
graphing calculators from Texas Instruments are supported.")
   (license license:gpl2+)))

(define-public gfm
  (package
   (name "gfm")
   (version "1.08")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilp/tilp2-linux/"
                                "tilp2-" tilp-version "/"
                                name "-" version ".tar.bz2"))
            (sha256
             (base32
              "0zq1a9mm54zr18dz2mqh79w1a126xwqz6dcrpjlbd1pnmg01l0q9"))
            (patches
             (list
              (local-file
               (string-append %patch-dir
                              "/gfm-remove-broken-kde-support.patch"))))))
   (build-system gnu-build-system)
   (native-inputs
    `(("autoconf" ,autoconf)
      ("automake" ,automake)
      ("gettext" ,gettext-minimal)
      ("libtool" ,libtool)
      ("pkg-config" ,pkg-config)))
   (inputs
    `(("libglade" ,libglade)
      ("libticalcs" ,libticalcs)))
   (home-page "http://lpg.ticalc.org/prj_tilp/")
   (synopsis "Group File Manager for TI handhelds")
   (description
    "The @code{GFM} is an application allowing to manipulate
single/group/tigroup files of all Texas Instruments handhelds.  It can create anew file, open an existing file, save a file, rename variables, remove
variables, create folders, group files into a group/tigroup file and ungroup a
group/tigroup file into single files.")
   (license license:gpl2+)))

(define-public tilp2
  (package
   (name "tilp2")
   (version tilp-version)
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilp/tilp2-linux/"
                                "tilp2-" tilp-version "/"
                                name "-" version ".tar.bz2"))
            (sha256
             (base32
              "0isf73bjwk06baz2gm3vpdh600gqck9ca4aqxzb089dmxriv6fkv"))
            (patches
             (list
              (local-file
               (string-append %patch-dir
                              "/tilp2-remove-broken-kde-support.patch"))))))
   (build-system gnu-build-system)
   (native-inputs
    `(("autoconf" ,autoconf)
      ("automake" ,automake)
      ("gettext" ,gettext-minimal)
      ("intltool" ,intltool)
      ("libtool" ,libtool)
      ("pkg-config" ,pkg-config)))
   (inputs
    `(("gfm" ,gfm)
      ("gtk" ,gtk+-2)
      ("libticalcs" ,libticalcs)))
   (home-page "http://lpg.ticalc.org/prj_tilp/")
   (synopsis "TI graphing calculator link/transfer program")
   (description
    "@code{TILP} is a program allowing a computer to communicate with TI
graphing calculators (TI-Z80, TI-68k and Nspire series).")
   (license license:gpl2+)))

(define-public tilem
  (package
   (name "tilem")
   (version "2.0")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/tilem/tilem/"
                                name "-" version ".tar.bz2"))
            (sha256
             (base32
              "1ba38xzhp3yf21ip3cgql6jzy49jc34sfnjsl4syxyrd81d269zw"))))
   (build-system gnu-build-system)
   (native-inputs
    `(("pkg-config" ,pkg-config)))
   (inputs
    `(("gtk" ,gtk+-2)
      ("libticalcs" ,libticalcs)))
   (arguments
    `(#:tests? #f ; no check target
      #:configure-flags '("LDFLAGS=-lm")))
   (home-page "http://lpg.ticalc.org/prj_tilem/")
   (synopsis "TI Z80 calculator emulator")
   (description
    "@code{TilEm} is an emulator and debugger for Texas Instruments Z80-based
graphing calculators.  It can emulate any of the following calculator models:
@enumerate
@item TI-73 / TI-73 Explorer
@item TI-76.fr
@item TI-81
@item TI-82
@item TI-82 STATS / TI-82 STATS.fr
@item TI-83
@item TI-83 Plus / TI-83 Plus Silver Edition / TI-83 Plus.fr
@item TI-84 Plus / TI-84 Plus Silver Edition / TI-84 pocket.fr
@item TI-85
@item TI-86
@end enumerate
TilEm fully supports all known versions of the above calculators (as of 2012),
and attempts to reproduce the behavior of the original calculator hardware as
faithfully as possible.")
   (license license:gpl3+)))

(define-public spasm-ng
  (package
   (name "spasm-ng")
   (version "0.5-beta.3")
   (source (origin
            (method url-fetch)
            (uri (string-append "https://github.com/alberthdev/spasm-ng/archive/v"
                               version ".tar.gz"))
            (sha256
             (base32
              "0sz8nrjbzmqr1m0am8yhaiis0msm6k89nk6lpqpq0m7bgjbminjk"))
            (file-name (string-append name "-" version ".tar.gz"))))
   (build-system gnu-build-system)
   (native-inputs
    `(("python" ,python))) ; The test script is written in Python.
   (inputs
    `(("gmp" ,gmp)
      ("openssl" ,openssl)))
   (arguments
    `(#:make-flags (list (string-append "DESTDIR=" (assoc-ref %outputs "out")))
      #:phases
      (modify-phases %standard-phases
        (delete 'configure) ; no configure script
        (add-before 'install 'create-bin-dir
          (lambda* (#:key outputs #:allow-other-keys)
            (mkdir-p (string-append (assoc-ref outputs "out") "/bin"))
            #t)))))
   (home-page "https://github.com/alberthdev/spasm-ng")
   (synopsis "z80/eZ80 assembler")
   (description
    "@code{SPASM-ng} is a z80/eZ80 assembler with extra features to support
development for TI calculators.")
   (license license:gpl2+)))
